package com.acttime.GenericLib;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class GetExcelLib {

	public String getExcelData(String sheetName, int rowNum, int colNum) throws InvalidFormatException, IOException
	{
		FileInputStream fis = new FileInputStream("C:\\Users\\Upendrra\\Desktop\\Excel Data\\data.xlsx");
		Workbook wb = WorkbookFactory.create(fis);
		Sheet sh = wb.getSheet(sheetName);
		Row row = sh.getRow(rowNum);
		Cell cel = row.getCell(colNum);
		String str = cel.getStringCellValue();
		return str;
	}
	
	public int getRowCount(String sheetName) throws InvalidFormatException, IOException
	{
		FileInputStream fis = new FileInputStream("C:\\Users\\Upendrra\\Desktop\\Excel Data\\data.xlsx");
		Workbook wb = WorkbookFactory.create(fis);
		Sheet sh = wb.getSheet(sheetName);
		int rowCount = sh.getLastRowNum();
		return rowCount;
	}
	
	public int getCellCount(String sheetName,int rowNum) throws InvalidFormatException, IOException
	{
		FileInputStream fis = new FileInputStream("C:\\Users\\Upendrra\\Desktop\\Excel Data\\data.xlsx");
		Workbook wb = WorkbookFactory.create(fis);
		Sheet sh = wb.getSheet(sheetName);
		Row row = sh.getRow(rowNum);
		int cellCount = row.getLastCellNum();
		return cellCount;
	}
	
	public void setExcelData(String sheetName,int rowNum, int colNum,String status) throws InvalidFormatException, IOException
	{
		FileInputStream fis = new FileInputStream("C:\\Users\\Upendrra\\Desktop\\data.xlsx");
		Workbook wb = WorkbookFactory.create(fis);
		Sheet sh = wb.getSheet(sheetName);
		Row row = sh.getRow(rowNum);
		Cell cel = row.createCell(colNum);
		cel.setCellType(Cell.CELL_TYPE_STRING);
		cel.setCellValue(status);
		FileOutputStream fos = new FileOutputStream("C:\\Users\\Upendrra\\Desktop\\data.xlsx");
		wb.write(fos);
	}
}


